/**
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 *
 * @data:2017年10月17日
 * @author:MaShibo
 */
package com.wttech.dzzf.process.service.blacklist;

import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.OBU;
import com.wtkj.vo.dzzf.base.kfzx.ListRealRecord;
import com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue;
import com.wttech.dzzf.process.dao.BlackListInfoDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.blacklist.CardBlackList;
import com.wttech.dzzf.process.model.blacklist.OBUBlackList;
import com.wttech.dzzf.process.model.user.Obu;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 黑名单信息创建
 *
 * @author MaShibo
 * @date 2017年10月17日
 */
@Service
public class BlackListCreatorService {


	private static final Logger LOGGER = LoggerFactory.getLogger(BlackListCreatorService.class);

	private static final int DEFAULT_PROCESS_NUM = 10000;

	private static final String DEFAULT_ISSUERNO = "610101";

	private static final int DEFAULT_OLD_CARD_BLTYPE = 3;

	@Resource
	private BlackListInfoDao blackListInfoDao;

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private ThreadPoolTaskExecutor executor;

	@Resource
	private BaseInfoService baseInfoService;

	private void createBlackListInfos() {
		blackListInfoDao.resetBlObuType1();
		int i = 1;
		while (true) {
			LOGGER.info("第{}次循环开始", i);
			List<ListRealRecord> records = blackListInfoDao.queryBllistRecordInfos(1, DEFAULT_PROCESS_NUM);
			if (CollectionUtils.isEmpty(records)) {
				executor.shutdown();
				LOGGER.info("处理完毕");
				return;
			}
			List<Future<Void>> futures = new ArrayList<>();
			records.forEach(record -> futures.add(executor.submit(() -> {
				try {
					processBListRecord(record);
				} catch (Exception e) {
					LOGGER.error(e.getMessage(), e);
				}
				return null;
			})));
			baseInfoService.getFutureResult(futures);
			i++;
			LOGGER.info("第{}次循环结束", i);
		}

	}

	private void processBListRecord(ListRealRecord record) {
		String cardId = record.getCardId();
		String obuId = record.getObuId();
		if (StringUtils.isNotBlank(cardId)) {
			processCardBlackList(record);
		}

		if (StringUtils.isNotBlank(obuId)) {
			processObuBlackList(record);
		}

		blackListInfoDao.updateBlObuType1(record.getListID(), 0);
	}

	private void processCardBlackList(ListRealRecord record) {
		String cardId = record.getCardId();
		Date optOn = record.getOptOn();
		String creationTime = DateFormatUtils.ISO_DATETIME_FORMAT.format(optOn == null ? record.getStartOn() : optOn);
		Integer type = null;
		CardInfo cardInfo = blackListInfoDao.queryCardInfoByNo(cardId);
		if (cardInfo != null) {
			Integer ecardStatus = cardInfo.getEcardStatus();
			if (ecardStatus == CardStaticValue.EcardStatus.EcardState_ZC) {
				LOGGER.info("卡号【{}】为正常状态", cardId);
			}
			if (ecardStatus == 2 || ecardStatus == 3) {
				//卡挂失
				type = 1;
			}
		}
		CardBlackList cb = new CardBlackList();
		cb.setIssuerId(DEFAULT_ISSUERNO);
		cb.setCreationTime(creationTime);
		if (type == null) {
			type = DEFAULT_OLD_CARD_BLTYPE;
		}
		cb.setType(type);
		cb.setCardId(record.getRoadNetId() + record.getCardId());
		// 进入黑名单
		cb.setStatus(1);
		try {
			transferUserInfoDao.createTestObjcet(cb);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.info(cb.toString());
		}
		blackListInfoDao.updateBlCardType(record.getListID(), type);
	}

	private void processObuBlackList(ListRealRecord record) {
		String obuId = record.getObuId();
		Obu obu = blackListInfoDao.queryBsObuById(obuId);
		if (obu == null) {
			LOGGER.info("OBU【{}】信息尚未上传", obuId);
		}
		Date optOn = record.getOptOn();
		String creationTime = DateFormatUtils.ISO_DATETIME_FORMAT.format(optOn == null ? record.getStartOn() : optOn);
		OBUBlackList ob = new OBUBlackList();
		ob.setIssuerId(DEFAULT_ISSUERNO);
		ob.setCreationTime(creationTime);
		//无签挂起
		ob.setType(2);
		ob.setObuId(record.getObuId());
		ob.setStatus(1);
		try {
			transferUserInfoDao.createTestObjcet(ob);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.info(ob.toString());
		}
		blackListInfoDao.updateBlCardType(record.getListID(), ob.getType());
	}


	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		BlackListCreatorService service = context.getBean(BlackListCreatorService.class);
		service.createBlackListInfos();
	}

}

